package com.qingke.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


/**
 * Servlet implementation class Upload
 */
@WebServlet("/Upload")
public class Upload extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Upload() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String fileDir = getServletContext().getRealPath("/img");
		 System.out.println(fileDir);
		 String fileName="";
		 String date="";
		 String type="";
		 int size=0;	 
		 String address="";
		 request.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8");
		 if(ServletFileUpload.isMultipartContent(request)){		 
			 
			 DiskFileItemFactory factory=new DiskFileItemFactory();
			 factory.setSizeThreshold(20*1024);
			 ServletFileUpload upload=new ServletFileUpload(factory);
			 int MaxSize=10*1024*1024;
			 List<FileItem> formlists=null;			 
			 try {
				//这里居然要完整的上传完成，才能继续下面的代码，用户体验太差
				formlists=upload.parseRequest(request);
			 } catch (FileUploadException e) {
				e.printStackTrace();
			 }
			 Iterator<FileItem> iter=formlists.iterator(); 
			 while(iter.hasNext()){		 
				 FileItem formItem=iter.next();				 
				 if(!formItem.isFormField()){
					 //获取路径
					 String path=formItem.getName();
					 //类型
					 type=formItem.getContentType();
					 //大小
					 size=(int) formItem.getSize();
					 //时间
					 System.out.println(path);
					 System.out.println(date);
					 System.out.println(type);
					 System.out.println(size);
					 if(size>MaxSize){
						 //文件大于10MB，上传失败！
						 
						 response.sendRedirect("failure.jsp?error=1");
						 break;
					 }
					 if(path==null||path.equals("")&&formItem.getSize()==0){
						 //文件为空，上传失败！
						 response.sendRedirect("failure.jsp?error=2");
						 break;
					 }
					 //名称
					 fileName=new File(path).getName();
					 //地址
					 address="upload/" + fileName;
					 System.out.println(address);
					 File saveFile=new File(fileDir+"\\"+fileName);
					 
					 try {
						formItem.write(saveFile);
					 } catch (Exception e) {
						e.printStackTrace();
					 }
					 response.sendRedirect("show.jsp?img="+fileName);
					 break;
				 }
			 }
		 }
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
